﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using MxTol.Models;
using MxTol.UI.Helpers;
using MxTol.Managers;
using System.Text.RegularExpressions;

namespace MxTol.UI.Controllers
{
    /// <summary>
    /// 财经法规案例分析题资料文字描述管理器
    /// </summary>
    public class FrAnalysisDescripeManagementController : Controller
    {
        private const string MIME_TYPE = "application/json;charset=utf-8;";

        #region View

        // 添加案例分析题资料文字描述
        public ActionResult AddDescripeView()
        {
            if (Session["account"] == null)
            {
                string script = string.Format("<script>window.top.location='{0}'</script>", ConfigurationManager.AppSettings["LoginUrl"]);
                return Content(script);
            }
            else
            {
                return View();
            }
        }

        // 修改案例分析题资料文字描述
        public ActionResult UpdateDescripeView()
        {
            if (Session["account"] == null)
            {
                string script = string.Format("<script>window.top.location='{0}'</script>", ConfigurationManager.AppSettings["LoginUrl"]);
                return Content(script);
            }
            else
            {
                string id = Request["id"];
                FrAnalysisDescripe descripe = null;
                try
                {
                    descripe = FrAnalysisDescripe.GetDescripeByID(new Guid(id));
                }
                catch (Exception ex)
                {
                    LogHelper.WriteLogAsync("修改财经法规案例分析题资料文字描述信息Action失败", ex);
                }

                if (descripe != null)
                {
                    descripe.Title = MethodHelper.ToOneLine(descripe.Title);
                    return View(descripe);
                }
                else
                {
                    return Content("获取案例分析题资料文字描述信息失败");
                }
            }
        }

        // 查询案例分析题资料文字描述
        public ActionResult QueryDescripeView()
        {
            if (Session["account"] == null)
            {
                string script = string.Format("<script>window.top.location='{0}'</script>", ConfigurationManager.AppSettings["LoginUrl"]);
                return Content(script);
            }
            else
            {
                return View();
            }
        }

        #endregion

        #region 功能

        // 以Json分页格式返回所有案例分析题资料文字描述
        public ActionResult GetFrDescripeJson()
        {
            string json = EasyuiHelper.DefaultJson;
            try
            {
                string currentPage = Request["page"];
                string pageSize = Request["rows"];

                json = FrAnalysisDescripeManager.GetAllDescripe(int.Parse(currentPage), int.Parse(pageSize));
            }
            catch (Exception ex)
            {
                json = EasyuiHelper.DefaultJson;
                LogHelper.WriteLogAsync("获取财经法规案例分析题资料文字描述题库Json格式数据错误", ex);
            }
            return Content(json, MIME_TYPE);
        }

        // 添加案例分析题资料文字描述
        public ActionResult AddDescripe()
        {
            // 0：异常失败  1：成功
            int flag = 0;
            try
            {
                string title = Request["title"];

                if (Session["account"] != null)
                {
                    FrAnalysisDescripe descripe = new FrAnalysisDescripe();

                    descripe.Title = title;
                    descripe.AddPerson = AdminManager.GetAdminName(Session["account"].ToString());
                    descripe.AddTime = DateTime.Now;

                    descripe.Save();
                    flag = 1;
                }
            }
            catch (Exception ex)
            {
                flag = 0;
                LogHelper.WriteLogAsync("添加财经法规案例分析题资料文字描述失败", ex);
            }

            return Content(flag.ToString(), MIME_TYPE);
        }

        // 删除案例分析题资料文字描述
        public ActionResult DeleteDescripe()
        {
            // 0:异常失败  1:成功
            int flag = 0;
            try
            {
                string id = Request["id"];
                FrAnalysisDescripe descripe = FrAnalysisDescripe.GetDescripeByID(new Guid(id));
                if (descripe != null)
                {
                    descripe.Delete();
                    flag = 1;
                }
            }
            catch (Exception ex)
            {
                flag = 0;
                LogHelper.WriteLogAsync("删除财经法规案例分析题资料文字描述失败", ex);
            }
            return Content(flag.ToString(), MIME_TYPE);
        }

        // 修改案例分析题资料文字描述
        public ActionResult UpdateDescripe()
        {
            // 0:异常失败  1:成功
            int flag = 0;
            try
            {
                if (Session["account"] == null)
                {
                    string script = string.Format("<script>window.top.location='{0}'</script>", ConfigurationManager.AppSettings["LoginUrl"]);
                    return Content(script);
                }

                string id = Request["id"];
                FrAnalysisDescripe descripe = FrAnalysisDescripe.GetDescripeByID(new Guid(id));
                if (descripe != null)
                {
                    descripe.Title = Request["title"];

                    descripe.Save();
                    flag = 1;
                }
            }
            catch (Exception ex)
            {
                flag = 0;
                LogHelper.WriteLogAsync("修改财经法规案例分析题资料文字描述失败", ex);
            }
            return Content(flag.ToString(), MIME_TYPE);
        }

        // 查询案例分析题资料文字描述
        public ActionResult QueryDescripe()
        {
            string json = EasyuiHelper.DefaultJson;
            try
            {
                string title = Request["title"];

                // 全部搜索
                int currentPage = 1;
                string pageSize = Request["rows"];

                // 拼接sql基本查询字段
                string sql_base = "select * from Fr_Analysis_Descripe";
                string sql_where = string.Empty;
                if (!string.IsNullOrEmpty(title))
                {
                    sql_where += string.Format("and descripe like '%{0}%' ", title);
                }

                // 有查询条件，没有查询所有
                string sql = sql_base;
                if (sql_where.Length != 0)
                {
                    // 去掉第一个"and"
                    if ((sql_where.TrimStart()).StartsWith("and"))
                    {
                        sql_where = sql_where.Substring(3, sql_where.Length - 3);
                        sql = sql_base + " where " + sql_where;
                    }
                }
                json = FrAnalysisDescripeManager.GetAllDescripe(currentPage, int.Parse(pageSize), sql);
            }
            catch (Exception ex)
            {
                json = EasyuiHelper.DefaultJson;
                LogHelper.WriteLogAsync("查询财经法规案例分析题资料文字描述错误", ex);
            }
            return Content(json, MIME_TYPE);
        }

        #endregion

    }
}
